package com.geek.service.impl;

import com.geek.bean.data.GoodsCollectData;
import com.geek.mapper.MarketCollectMapper;
import com.geek.mapper.MarketGoodsMapper;
import com.geek.model.MarketCollect;
import com.geek.model.MarketCollectExample;
import com.geek.model.MarketGoods;
import com.geek.service.CollectService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 小豪
 * @version 1.0
 * @description
 * @date 2024-06-17 17:43
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private MarketCollectMapper collectMapper;
    @Autowired
    private MarketGoodsMapper goodsMapper;

    /**
     * 根据goodsId统计该商品被几个用户收藏
     */
    @Override
    public Integer hasCollection(Integer goodsId, byte valueType, int userId) {
        MarketCollectExample example = new MarketCollectExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andUserIdEqualTo(userId)
                .andValueIdEqualTo(goodsId)
                .andTypeEqualTo(valueType);
        example.setOrderByClause("add_time desc");
        List<MarketCollect> marketCollects = collectMapper.selectByExample(example);
        if (marketCollects != null && marketCollects.size() == 1) {
            return 1;
        } else {
            return 0;
        }
    }


    /**
     * 查询收藏列表
     */
    @Override
    public List<GoodsCollectData> list(Integer userId, Byte type, Integer page, Integer limit) {
        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.createCriteria().andDeletedEqualTo(false)
                .andUserIdEqualTo(userId).andTypeEqualTo(type);
        PageHelper.startPage(page, limit);
        List<MarketCollect> collectList = collectMapper.selectByExample(collectExample);
        List<GoodsCollectData> collectDataList = new ArrayList<>();
        collectList.forEach(collect -> {
            Integer goodsId = collect.getValueId();
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodsId);
            GoodsCollectData goodsCollectData = new GoodsCollectData();
            goodsCollectData.setId(collect.getId());
            goodsCollectData.setBrief(marketGoods.getBrief());
            goodsCollectData.setPicUrl(marketGoods.getPicUrl());
            goodsCollectData.setRetailPrice(marketGoods.getRetailPrice());
            goodsCollectData.setValueId(marketGoods.getGoodsSn());
            goodsCollectData.setName(marketGoods.getName());
            goodsCollectData.setType(type);
            collectDataList.add(goodsCollectData);
        });
        return collectDataList;
    }

    /**
     * 商品收藏
     */
    @Override
    public void andOrDeleteGoods(Integer userId, Byte type, Integer valueId) {
        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.createCriteria().andDeletedEqualTo(false)
                .andTypeEqualTo(type)
                .andUserIdEqualTo(userId)
                .andValueIdEqualTo(valueId);
        Date currDate = new Date();
        List<MarketCollect> collectList = collectMapper.selectByExample(collectExample);
        if (collectList.isEmpty()) {
            MarketCollect marketCollect = new MarketCollect();
            marketCollect.setUserId(userId);
            marketCollect.setType(type);
            marketCollect.setAddTime(currDate);
            marketCollect.setUpdateTime(currDate);
            marketCollect.setValueId(valueId);
            marketCollect.setDeleted(false);
            collectMapper.insertSelective(marketCollect);
        } else {
            MarketCollect marketCollect = collectList.get(0);
            MarketCollect updateCollect = new MarketCollect();
            updateCollect.setId(marketCollect.getId());
            updateCollect.setUpdateTime(currDate);
            updateCollect.setDeleted(true);
            collectMapper.updateByPrimaryKeySelective(updateCollect);
        }
    }
}
